package com.agileboot.attendance.controller;

import com.agileboot.attendance.domain.overtime.dto.OvertimeDTO;
import com.agileboot.attendance.domain.overtime.dto.OvertimeQueryDTO;
import com.agileboot.attendance.domain.overtime.service.OvertimeApplicationService;
import com.agileboot.common.core.page.PageDTO;
import com.agileboot.common.core.dto.ResponseDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 加班申请Controller
 *
 * @author ruoyi
 */
@Api(tags = "加班申请管理")
@RestController
@RequestMapping("/attendance/overtime")
@RequiredArgsConstructor
public class OvertimeController {

    private final OvertimeApplicationService overtimeApplicationService;

    /**
     * 分页查询加班申请列表
     */
    @ApiOperation("查询加班申请列表")
    @GetMapping("/list")
    public ResponseDTO<PageDTO<OvertimeDTO>> list(OvertimeQueryDTO queryDTO,
                                                  @RequestParam(defaultValue = "1") Integer pageNum,
                                                  @RequestParam(defaultValue = "10") Integer pageSize) {
        queryDTO.setPageNum(pageNum);
        queryDTO.setPageSize(pageSize);
        PageDTO<OvertimeDTO> pageDTO = overtimeApplicationService.getPage(queryDTO);
        return ResponseDTO.ok(pageDTO);
    }

    /**
     * 获取加班申请详情
     */
    @ApiOperation("获取加班申请详情")
    @GetMapping("/{overtimeId}")
    public ResponseDTO<OvertimeDTO> getInfo(@PathVariable Long overtimeId) {
        OvertimeDTO overtime = overtimeApplicationService.getInfo(overtimeId);
        return ResponseDTO.ok(overtime);
    }

    /**
     * 提交加班申请
     */
    @ApiOperation("提交加班申请")
    @PostMapping("/submit")
    public ResponseDTO<Long> submit(@Valid @RequestBody OvertimeDTO overtimeDTO) {
        Long overtimeId = overtimeApplicationService.submit(overtimeDTO);
        return ResponseDTO.ok(overtimeId);
    }

    /**
     * 修改加班申请
     */
    @ApiOperation("修改加班申请")
    @PutMapping
    public ResponseDTO<Void> update(@Valid @RequestBody OvertimeDTO overtimeDTO) {
        overtimeApplicationService.update(overtimeDTO);
        return ResponseDTO.ok();
    }

    /**
     * 删除加班申请
     */
    @ApiOperation("删除加班申请")
    @DeleteMapping("/{overtimeId}")
    public ResponseDTO<Void> delete(@PathVariable Long overtimeId) {
        overtimeApplicationService.cancel(overtimeId);
        return ResponseDTO.ok();
    }

    /**
     * 审批加班申请
     */
    @ApiOperation("审批加班申请")
    @PostMapping("/approve/{overtimeId}")
    public ResponseDTO<Void> approve(@PathVariable Long overtimeId,
                        @RequestParam Long approverId,
                        @RequestParam String approverName,
                        @RequestParam String status,
                        @RequestParam(required = false) String comment) {
        overtimeApplicationService.approve(overtimeId, approverId, approverName, status, comment);
        return ResponseDTO.ok();
    }

    /**
     * 取消加班申请
     */
    @ApiOperation("取消加班申请")
    @PostMapping("/cancel/{overtimeId}")
    public ResponseDTO<Void> cancel(@PathVariable Long overtimeId) {
        overtimeApplicationService.cancel(overtimeId);
        return ResponseDTO.ok();
    }
}
